﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rbdesign
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void הוסףמשתמשToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fClearUserTextBoxs();
            pnlArticle.Visible = false;
            pnlUser.Visible = true;
            pnlUserControls.Visible = true;
            lblUserTitle.Text = "הוסף משתמש";
            comboUserChooseUsername.Visible = false;
            btnUserEdit.Visible = false;
            btnUserDelete.Visible = false;
            txtUserUsername.Visible = true;
            btnUserAdd.Visible = true;
        }

        private void ערוךמשתמשToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fClearUserTextBoxs();
            comboUserChooseUsername.DataSource =new BindingList<string>(Controller.ControllerUser.fFillUserComboBox());
            pnlArticle.Visible = false;
            pnlUser.Visible = true;
            pnlUserControls.Visible = true;
            lblUserTitle.Text = "ערוך משתמש";
            btnUserDelete.Visible = false;
            txtUserUsername.Visible = false;
            btnUserAdd.Visible = false;
            comboUserChooseUsername.Visible = true;
            btnUserEdit.Visible = true;
        }

        private void מחקמשתמשToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fClearUserTextBoxs();
            comboUserChooseUsername.DataSource = new BindingList<string>(Controller.ControllerUser.fFillUserComboBox());
            pnlArticle.Visible = false;
            pnlUser.Visible = true;
            pnlUserControls.Visible = false;
            lblUserTitle.Text = "מחק משתמש";
            txtUserUsername.Visible = false;
            btnUserAdd.Visible = false;
            btnUserEdit.Visible = false;
            btnUserDelete.Visible = true;
            comboUserChooseUsername.Visible = true;
        }

        private void הוסףמאמרToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlArticle.Visible = true;
            pnlArticleNew.Visible = true;
            pnlUser.Visible = false;
            lblArticleTitle.Text = "הוסף מאמר";
            lblArticleChooseTopic.Text = "בחר הודעה";
            btnArticleAdd.Visible = true;
            btnArticleClear.Visible = true;
            btnArticleDelete.Visible = false;
            fClearArticleTextBoxs();
        }

        private void מחקמאמרToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pnlArticle.Visible = true;
            pnlArticleNew.Visible = false;
            pnlUser.Visible = false;
            lblArticleTitle.Text = "מחק מאמר";
            lblArticleChooseTopic.Text = "בחר מאמר";
            btnArticleAdd.Visible = false;
            btnArticleClear.Visible = false;
            btnArticleDelete.Visible = true;
            fClearArticleTextBoxs();
        }

        private void btnUserNewClose_Click(object sender, EventArgs e)
        {
            pnlUser.Visible = false;
        }

        private void btnArticleClose_Click(object sender, EventArgs e)
        {
            pnlArticle.Visible = false;
        }

        private void btnUserClear_Click(object sender, EventArgs e)
        {
            fClearUserTextBoxs();
            comboUserChooseUsername.DataSource = new BindingList<string>(Controller.ControllerUser.fFillUserComboBox());
            radioUserSexMale.Checked = true;
        }

        private void btnArticleClear_Click(object sender, EventArgs e)
        {
            fClearArticleTextBoxs();
        }

        private void btnUserAdd_Click(object sender, EventArgs e)
        {
            char sex='M';
            if (radioUserSexFemale.Checked)
                sex='F';
            string interests="";
            if (chkUserInterestsCinema.Checked)
                interests+="Cinema,";
            if (chkUserInterestsParties.Checked)
                interests+="Parties,";
            if (chkUserInterestsSport.Checked)
                interests+="Sport,";
            interests=interests.Trim();
            if (interests!="")
                interests = interests.Substring(0, interests.Length - 1);   // Cut off last comma ,

            int result = Controller.ControllerUser.fCreateNewUser(txtUserUsername.Text, txtUserPassword.Text, txtUserEmail.Text, dateUserBirthdate.Value.ToShortDateString(), sex, interests);

            MessageBox.Show(Controller.ControllerUser.fCreateNewUserMessage(result));

            if (result==0)
            {
                fClearUserTextBoxs();
            }
        }

        private void btnArticleAdd_Click(object sender, EventArgs e)
        {
            int topic = int.Parse(comboArticleChooseTopic.SelectedValue.ToString());
            int subject = int.Parse(comboArticleChooseSubject.SelectedValue.ToString());
            string title = txtArticleTitle.Text.Trim();

            int result = Controller.ControllerArticle.fCreateNewArticle(title, subject, topic);

            MessageBox.Show(Controller.ControllerArticle.fCreateNewArticleMessage(result));

            if (result == 0)
            {
                fClearArticleTextBoxs();
            }
        }

        private void btnUserEdit_Click(object sender, EventArgs e)
        {
            char sex = 'M';
            if (radioUserSexFemale.Checked)
                sex = 'F';
            string interests = "";
            if (chkUserInterestsCinema.Checked)
                interests += "Cinema,";
            if (chkUserInterestsParties.Checked)
                interests += "Parties,";
            if (chkUserInterestsSport.Checked)
                interests += "Sport,";
            interests = interests.Trim();
            if (interests != "")
                interests = interests.Substring(0, interests.Length - 1);   // Cut off last comma ,

            int result = Controller.ControllerUser.fEditUserDetails(comboUserChooseUsername.SelectedValue.ToString(), txtUserPassword.Text, txtUserEmail.Text, dateUserBirthdate.Value.ToShortDateString(), sex, interests);

            MessageBox.Show(Controller.ControllerUser.fCreateNewUserMessage(result));
        }

        private void btnUserDelete_Click(object sender, EventArgs e)
        {
            int result = Controller.ControllerUser.fDeleteUser(comboUserChooseUsername.SelectedValue.ToString());

            fClearUserTextBoxs();
            comboUserChooseUsername.DataSource = new BindingList<string>(Controller.ControllerUser.fFillUserComboBox());

            MessageBox.Show(Controller.ControllerUser.fDeleteUserMessage(result));
        }

        private void btnArticleDelete_Click(object sender, EventArgs e)
        {
            int article = int.Parse(comboArticleChooseTopic.SelectedValue.ToString());

            int result = Controller.ControllerArticle.fDeleteArticle(article);

            fClearArticleTextBoxs();

            MessageBox.Show(Controller.ControllerArticle.fDeleteArticleMessage(result));
        }

        private void fClearUserTextBoxs()
        {
            txtUserUsername.Text = "";
            txtUserEmail.Text = "";
            txtUserPassword.Text = "";
            chkUserInterestsSport.Checked = false;
            chkUserInterestsParties.Checked = false;
            chkUserInterestsCinema.Checked = false;
        }

        private void fClearArticleTextBoxs()
        {
            comboArticleChooseSubject.DataSource = new BindingSource(Controller.ControllerSubject.fFillArticleSubjectComboBox(),null);
            comboArticleChooseSubject.DisplayMember = "Value";
            comboArticleChooseSubject.ValueMember = "Key";
            if (lblArticleTitle.Text == "הוסף מאמר")
                comboArticleChooseTopic.DataSource = new BindingSource(Controller.ControllerTopic.fFillArticleTopicComboBox(), null);
            else
                comboArticleChooseTopic.DataSource = new BindingSource(Controller.ControllerArticle.fFillArticleComboBox(), null);
            comboArticleChooseTopic.DisplayMember = "Value";
            comboArticleChooseTopic.ValueMember = "Key";
            txtArticleTitle.Text = "";
        }

        private void comboUserChooseUsername_SelectedValueChanged(object sender, EventArgs e)
        {
            fClearUserTextBoxs();
            if (comboUserChooseUsername.SelectedValue.ToString() != "Choose:" && btnUserEdit.Visible)
            {
                Model.User uUserDetails = Controller.ControllerUser.fGetUserDetails(comboUserChooseUsername.SelectedValue.ToString());
                txtUserPassword.Text = uUserDetails.password;
                txtUserEmail.Text = uUserDetails.email;

                dateUserBirthdate.Value = Convert.ToDateTime(uUserDetails.birthdate);

                if (uUserDetails.sex == 'M')
                    radioUserSexMale.Checked = true;
                else
                    radioUserSexFemale.Checked = true;

                if (uUserDetails.interests != "")
                {
                    string[] arrInterests = uUserDetails.interests.Split(',');
                    foreach (string s in arrInterests)
                    {
                        if (s == "Cinema")
                            chkUserInterestsCinema.Checked = true;
                        else if (s == "Parties")
                            chkUserInterestsParties.Checked = true;
                        else if (s == "Sport")
                            chkUserInterestsSport.Checked = true;
                    }
                }
            }
        }
    }
}
